package com.mirabelle.juice.controller;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.event.ActionEvent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.mirabelle.juice.beans.Paginator;
import com.mirabelle.juice.model.Recipe;
import com.mirabelle.juice.service.RecipeService;

@Controller("C_RECIPE_SEARCH")
@Scope("session")
public class CRecipeSearch implements Serializable {
	private static final long	serialVersionUID	= 1L;
	private transient Logger	log					= LoggerFactory.getLogger(getClass());

	private List<Recipe>		recipes			= null;
	private String 				keyword			= null;
	private Paginator<Recipe>   paginator		= null;
	
	@Autowired
	@Qualifier("RecipeService")
	protected RecipeService	recipeService;
	@Autowired
	@Qualifier("C_LOGGED_AUTHOR")
	private CPreferences authorPreferences;
	
	@PostConstruct
	public void init() {
		log.debug("Initialisation du controlleur C_RECIPE_SEARCH.");
		recipes = recipeService.findAll();
		paginator = new Paginator<Recipe>(recipes, 4);
	}
	
	public void searchRecipe(ActionEvent e) {
		if ((null == keyword) || (keyword.length() == 0)) {
			log.debug("Appel de la methode searchRecipe sans mot clef : ");
			recipes = recipeService.findAll();
		} else {
			log.debug("Appel de la methode searchRecipe avec le mot clef : " + keyword);
			recipes = recipeService.findRecipeByKeyword("%" + keyword + "%");
		}
		paginator.setList(recipes);
	}
	
	public void searchAuthorRecipe(ActionEvent e) {
		log.debug("Appel de la methode searchAuthorRecipe pour l'auteur connecté : " + authorPreferences.getLoggedAuthor().getMail());
		recipes = recipeService.findRecipeByAuthor(authorPreferences.getLoggedAuthor());
		paginator.setList(recipes);
	}
	
	public String searchAuthorRecipe() {
		log.debug("Appel de la methode searchAuthorRecipe pour l'auteur connecté : " + authorPreferences.getLoggedAuthor().getMail());
		recipes = recipeService.findRecipeByAuthor(authorPreferences.getLoggedAuthor());
		paginator.setList(recipes);
		return "recipe";
	}
	
	public void searchRecentRecipe(ActionEvent e) {
		log.debug("Appel de la methode searchRecentRecipe");
		recipes = recipeService.findRecentRecipe();
		paginator.setList(recipes);
	}
	
	public void searchRandomRecipe(ActionEvent e) {
		log.debug("Appel de la methode searchRandomRecipe");
		recipes = recipeService.findRandomRecipe();
		paginator.setList(recipes);
	}
	
	public String searchRecipe() {
		if ((null == keyword) || (keyword.length() == 0)) {
			log.debug("Appel de la methode searchRecipe sans mot clef.");
			recipes = recipeService.findAll();
		} else {
			log.debug("Appel de la methode searchRecipe avec le mot clef : " + keyword);
			recipes = recipeService.findRecipeByKeyword("%" + keyword + "%");
		}
		paginator.setList(recipes);
		return "recipe";
	}
	
	public Paginator<Recipe> getPaginator() {
		return paginator;
	}
	
	public boolean isNext() {
		return paginator.hasNext();
	}
	
	public boolean isPrevious() {
		return paginator.hasPrevious();
	}
	
	public void getNext() {
		paginator.getNext();
	}
	
	public void getPrevious() {
		paginator.getPrevious();
	}

	public List<Recipe> getRecipes() {
		return recipes;
	}

	public void setRecipes(List<Recipe> recipes) {
		this.recipes = recipes;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	
	public CPreferences getauthorPreferences() {
		return authorPreferences;
	}
}
